package com.ddl.kafka.config.producer;

import com.ddl.kafka.config.dto.MessageData;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.concurrent.ExecutionException;

@Component
public class Demo01Producer {

	@Value("${msgQueue.topic}")
	private String topic;

	@Autowired
	private KafkaTemplate<Object, Object> kafkaTemplate;

	public SendResult syncSend(Long id) throws ExecutionException, InterruptedException {
		// 创建 Demo01Message 消息
		MessageData message = new MessageData();
		message.setId(id);
		// 同步发送消息
		return kafkaTemplate.send(topic, message).get();
	}

	public ListenableFuture<SendResult<Object, Object>> asyncSend(Long id) {
		// 创建 Demo01Message 消息
		MessageData message = new MessageData();
		message.setId(id);
		// 异步发送消息
		return kafkaTemplate.send(topic, message);
	}

}
